var MessageShow = function(msg){
	//提示信息
	alert(msg);
};

var BindImgEvent = function(){
	$(".Upload-img .textarea-div .button-save").bind({
		click:function(e){
			var b = e.target;
			var css = {
				"display":"none"
			};
			var css2= {
				"display":"block"
			};
			$(b).parent().css(css);
			$(b).parent().parent().children("p").css(css2).html($(b).parent().children("textarea").val());
		}
	});
	
	
	$(".Upload-img .textarea-div .button-cancel").bind({
		click:function(e){
			var b = e.target;
			var css = {
				"display":"none"
			};
			var css2= {
				"display":"block"
			};
			$(b).parent().css(css);
			$(b).parent().parent().children("p").css(css2);
	
		}
	});
	
	$(".Upload-img p[name=descrip]").bind({
		mouseover:function(e){
			var p = e.target;
			var css = {
				"backgroundColor":"#80F578",
				"color":"#ffffff"
			};
			$(p).css(css);
		},
		mouseout:function(e){
			var p = e.target;
			var css = {
				"backgroundColor":"#ffffff",
				"color":"#00ca00"
			};
			$(p).css(css);
		},
		click:function(e){
			var p = e.target;
			var css = {
				"display":"none"
			};
			var css2 = {
				"display":"block"
			}
			$(p).css(css);
			$(p).next().css(css2);
			$(p).next().children("textarea").val($(p).text()).focus();
		}
	});
};





jQuery.extend({
	/** * @see 将json字符串转换为对象 * @param json字符串 * @return 返回object,array,string等对象 */
	evalJSON : function(strJson) {
		return eval("(" + strJson + ")");
	}
});

jQuery.extend({
	/** * @see 将javascript数据类型转换为json字符串 * @param 待转换对象,支持object,array,string,function,number,boolean,regexp * @return 返回json字符串 */
	toJSON : function(object) {

		var type = typeof object;
		if ('object' == type) {
			if (Array == object.constructor)
				type = 'array';
			else if (RegExp == object.constructor)
				type = 'regexp';
			else
				type = 'object';
		}
		switch (type) {
			case 'undefined':
			case 'unknown':
				return;
				break;
			case 'function':
			case 'boolean':
			case 'regexp':
				return object.toString();
				break;
			case 'number':
				return isFinite(object) ? object.toString() : 'null';
				break;
			case 'string':
				return '"' + object.replace(/(\\|\")/g, "\\$1").replace(/\n|\r|\t/g, function() {
					var a = arguments[0];
					return (a == '\n') ? '\\n' : (a == '\r') ? '\\r' : (a == '\t') ? '\\t' : ""
				}) + '"';
				break;
			case 'object':
				if (object === null)
					return 'null';
				var results = [];
				for (var property in object) {
					var value = jQuery.toJSON(object[property]);
					if (value !== undefined)
						results.push(jQuery.toJSON(property) + ':' + value);
				}
				return '{' + results.join(',') + '}';
				break;
			case 'array':
				var results = [];
				for (var i = 0; i < object.length; i++) {
					var value = jQuery.toJSON(object[i]);
					if (value !== undefined)
						results.push(value);
				}
				return '[' + results.join(',') + ']';
				break;
		}
	}
});

var bindItemClick = function(){
		$(".dropdown-menu li").bind({
		click:function(e){
			var b = e.target;
			$(b).parent().parent().parent().children(".dropdown-toggle").html($(b).html()+"<b class='caret'></b>");
			$(b).parent().parent().parent().children(".dropdown-toggle").attr("tagid",$(b).parent().attr("tagid"));
		}
			
	});
};

var bindItemAlbumClick = function(){
		$("#dropdown-album .dropdown-menu li").bind({
		click:function(e){
			var b = e.target;
			$(b).parent().parent().parent().children(".dropdown-toggle").html($(b).html()+"<b class='caret'></b>");
			$(b).parent().parent().parent().children(".dropdown-toggle").attr("tagid",$(b).parent().attr("tagid"));
			if($(b).html() == "新建相册"){
				$("#New-Album").css("display","block");
			}
			else{
				$("#New-Album").css("display","none");
			}
		}
	});
};

$("#issue-button").bind({
	click:function(e){
		//获取数据
		var parm = {};
		parm.position = $("#text-position").val();
		parm.descrip = $("#text-descrip").val();
		parm.uid = "1";   //测试用
		parm.size = $(".img-info").size();
		parm.type_cont = $("#dropdown-cont .dropdown-toggle").attr("tagid");
		parm.type_style = $("#dropdown-style .dropdown-toggle").attr("tagid");
		if(parm.type_cont == undefined && parm.type_style == undefined){
			//提示不能为空
			MessageShow("风格信息不能为空");
			return;
		}
		//判断是否为新相册
		var AlbumID = $("#dropdown-album .dropdown-toggle").attr("tagid");
		if(AlbumID ==  undefined || AlbumID == "-1"){
			if($("#text-album").val() == ""){
				//错误 相册不能为空
				MessageShow("相册信息不能为空");
				return;
			}
			parm.isNewAlbum = "1";
			parm.AlbumName = $("#text-album").val();
		}
		else{
			parm.isNewAlbum = "0";
			parm.AlbumName = parm.type_style = $("#dropdown-album .dropdown-toggle").attr("tagid");
		}
		parm.imgInfo = [];
		var imgs = $(".img-info");
		for(var i = 0; i < parm.size; i++ ){
			var info = {};
			info.Pdiscrip = $(imgs[i]).find(".img-discrip").html();
			info.PID = $(imgs[i]).attr("pid");
			parm.imgInfo.push(info);
		}
		
		$.ajax({
                    url: "AJAX/Upload.ashx?op=IssueMulti",
                    type: 'POST',
                    //data:$.toJSON(parm),
                    data:parm,
                    dataType: "json",
                    success: function (json) {
                        if (json.success === "true") {
                        	//成功并且跳转
                            
                            MessageShow("发布成功");
                            window.location.href="Default.aspx";
                        }
                        else {
                            
                            MessageShow("发布失败");
                        }

                    }
               });
		
	}
});

var BindBoard = {
	Album:function(json){
		var b = $("#dropdown-album .dropdown-menu");
		for(var e in json.data){
		    var text = "<li tagid ='" + json.data[e].aid + "' >";
		    text += "<a>" + json.data[e].name + "</a></li>"
			//插入到Html中
			b.append(text);
		};
		
	},
	TypeCont:function(json){
		var b = $("#dropdown-cont .dropdown-menu");
		for(var e in json.data){
			var text = "<li tagid ='"+json.data[e].ID+"' >";
			text +="<a>"+json.data[e].TagName +"</a></li>"
			//插入到Html中
			b.append(text);
		};
	},
	TypeStyle:function(json){
		var b = $("#dropdown-style .dropdown-menu");
		for(var e in json.data){
			var text = "<li tagid ='"+json.data[e].ID+"' >";
			text +="<a>"+json.data[e].TagName +"</a></li>"
			//插入到Html中
			b.append(text);
		};
	}
};

(function($){
	//绑定DropDown  相册
	$.ajax({
		 url: "AJAX/GetUploadInfo.ashx?op=getAlbum",
         type: 'GET',
         data:"",
         dataType: "json",
         success:function(json){
         	if(json.success === "true"){
         		BindBoard.Album(json);
         		bindItemAlbumClick();
         	}
         	else{
         		//弹框
         		MessageShow("获取相册信息失败，请刷新重试");
         	}
         }
	});
	//绑定类型 内容
	$.ajax({
		 url: "AJAX/GetUploadInfo.ashx?op=getTpyeCont",
         type: 'GET',
         data:"",
         dataType: "json",
         success:function(json){
         	if(json.success === "true"){
         		BindBoard.TypeCont(json);
         		bindItemClick();
         	}
         	else{
         		//弹框
         		MessageShow("获取类型信息失败，请刷新重试");
         	}
         }
	});
	
	//绑定类型 风格
	$.ajax({
		 url: "AJAX/GetUploadInfo.ashx?op=getTpyeStyle",
         type: 'GET',
         data:"",
         dataType: "json",
         success:function(json){
         	if(json.success === "true"){
         		BindBoard.TypeStyle(json);
         		bindItemClick();
         	}
         	else{
         		//弹框
         		MessageShow("获取类型信息失败，请刷新重试");
         	}
         }
	});
	

})(jQuery)
